--[[
	Galactica
	File: protections.lua
]]

Galactica.Protections = {}
Galactica.Protections.Props = {}

Galactica.Protections.Undo = {}
Galactica.Protections.Undo.Create = undo.Create
Galactica.Protections.Undo.AddEntity = undo.AddEntity
Galactica.Protections.Undo.SetPlayer = undo.SetPlayer
Galactica.Protections.Undo.Finish = undo.Finish

function Galactica.Protections.SetPropOwner(ply, ent)
	-- Error("lol1")
    -- for k, v in pairs(Galactica.Protections.Props) do
		-- if(v[1] == ply:SteamID() and v[2] == ent) then
		-- Error("lol2")
			-- return
		-- end
	-- end
	-- Error("lol3")
	
	table.insert(Galactica.Protections.Props, {ply:SteamID(), ent})
	ent:SetNetworkedString("Galactica.Protections.Owner", ply:Nick())
end

function Galactica.Protections.GetPropOwner(ent)
	local Owner = ent:GetNetworkedString("Galactica.Protections.Owner")
	if(Owner == "") then
		Owner = "World"
	end
	return Owner
end

function Galactica.Protections.CheckPropOwner(ply, ent)
	if(ply:IsAdmin()) then return true end
	for k, v in pairs(Galactica.Protections.Props) do
		if(v[1] == ply:SteamID() and v[2] == ent) then
			return true
		end
	end
	return false
end

-- :D
-- if(cleanup) then
	-- local Clean = cleanup.Add
	-- function cleanup.Add(Player, Type, Entity)
		-- if(Entity) then
			-- local Check = Player:IsPlayer()
			-- local Valid = Entity:IsValid()
		    -- if(Check and Valid) then
		        -- Galactica.Protections.SetPropOwner(Player, Entity)
		    -- end
		-- end
	    -- Clean(Player, Type, Entity)
	-- end
-- end

-- local Meta = FindMetaTable("Player")
-- if(Meta.AddCount) then
	-- local Backup = Meta.AddCount
	-- function Player:AddCount(Type, Entity)
		-- alactica.Protections.SetPropOwner(self, Entity)
		-- Backup(self, Type, Entity)
	-- end
-- end

function undo.Create(name)
    Galactica.Protections.Undo.Current = {}
	Galactica.Protections.Undo.Current.Player = false
    Galactica.Protections.Undo.Current.Elements = {}
    Galactica.Protections.Undo.Create(name)
end

function undo.AddEntity(ent)
    if(Galactica.Protections.Undo.Current and ent and ent:IsValid()) then
        table.insert(Galactica.Protections.Undo.Current.Elements, ent)
    end
    Galactica.Protections.Undo.AddEntity(ent)
end

function undo.SetPlayer(ply)    
    if(Galactica.Protections.Undo.Current and ply and ply:IsValid()) then
        Galactica.Protections.Undo.Current.Player = ply
    end  
    Galactica.Protections.Undo.SetPlayer(ply)
end

function undo.Finish()
    if(Galactica.Protections.Undo.Current and Galactica.Protections.Undo.Current.Player != false and Galactica.Protections.Undo.Current.Elements) then 
        for k, ent in pairs(Galactica.Protections.Undo.Current.Elements) do
			Galactica.Protections.SetPropOwner(Galactica.Protections.Undo.Current.Player, ent)
        end
    end  
    Galactica.Protections.Undo.Finish()
end

function Galactica.Protections.PropRemoval(SteamID, PlayerName)
	for k, v in pairs(Galactica.Protections.Props) do
		if(v[1] == ply:SteamID() and v[2]:IsValid()) then
			v[2]:Remove()
		end
	end
	Galactica.NofityAll(tostring(PlayerName).."'s props have been cleaned up")
end

function Galactica.Protections.PlayerInitialSpawn(ply)
	local TimerName = "Galactica.Protections.PropRemoval: "..ply:SteamID()
	if(timer.IsTimer(TimerName)) then
		timer.Remove(TimerName)
	end
end
hook.Add("PlayerInitialSpawn", "Galactica.Protections.PlayerInitialSpawn", Galactica.Protections.PlayerInitialSpawn)

function Galactica.Protections.PlayerDisconnected(ply)
	timer.Create("Galactica.Protections.PropRemoval: "..ply:SteamID(), Galactica_PropRemovalDelay, 1, Galactica.Protections.PropRemoval, ply:SteamID(), ply:Nick())
end
hook.Add("PlayerDisconnected", "Galactica.Protections.PlayerDisconnected", Galactica.Protections.PlayerDisconnected)

function Galactica.Protections.CanTool(ply, tr, mode)
	if(tr.HitWorld) then return end
	ent = tr.Entity
	if(!ent:IsValid() or ent:IsPlayer()) then return false end
	if(!Galactica.Protections.CheckPropOwner(ply, ent)) then
		Galactica.Nofity(ply, "This prop belongs to "..Galactica.Protections.GetPropOwner(ent))
		return false
	elseif(toolgun == "nail") then
		local Trace = {}
		Trace.start = tr.HitPos
		Trace.endpos = tr.HitPos + (ply:GetAimVector() * 16.0)
		Trace.filter = {ply, tr.Entity}
		local tr2 = util.TraceLine(Trace)
		if(tr2.Hit && !tr2.Entity:IsPlayer()) then
			if(!Galactica.Protections.CheckPropOwner(ply, tr2.Entity)) then
				Galactica.Nofity(ply, "This prop belongs to "..Galactica.Protections.GetPropOwner(tr2.Entity))
				return false
			end
		end
	end	
end
hook.Add("CanTool", "Galactica.Protections.CanTool", Galactica.Protections.CanTool)

function Galactica.Protections.GravGunPhysgunStuff(ply, ent)
	if(!ent:IsValid()) then return end
	if(ent:IsPlayer() and ply:IsAdmin() == false) then return false end
	if(!Galactica.Protections.CheckPropOwner(ply, ent)) then
		Galactica.Nofity(ply, "This prop belongs to "..Galactica.Protections.GetPropOwner(ent))
		return false
	end
end
hook.Add("GravGunPickupAllowed", "Galactica.Protections.GravGunPickupAllowed", Galactica.Protections.GravGunPhysgunStuff)
hook.Add("GravGunPunt", "Galactica.Protections.GravGunPunt", Galactica.Protections.GravGunPhysgunStuff)
hook.Add("PhysgunPickup", "Galactica.Protections.PhysgunPickup", Galactica.Protections.GravGunPhysgunStuff)

function Galactica.Protections.OnPhysgunReload(weapon, ply)
	local tr = util.TraceLine(util.GetPlayerTrace(ply))
	if(!tr.HitNonWorld || !tr.Entity:IsValid() || tr.Entity:IsPlayer()) then return end
	if(!Galactica.Protections.CheckPropOwner(ply, tr.Entity)) then
		Galactica.Nofity(ply, "This prop belongs to "..Galactica.Protections.GetPropOwner(tr.Entity))
		return false
	end
end
hook.Add("OnPhysgunReload", "Galactica.Protections.OnPhysgunReload", Galactica.Protections.OnPhysgunReload)
